﻿using Bootstrap.Definition.Enums;
using Bootstrap.Helper;
using Bootstrap.Template.Button;
using Bootstrap.Template.TemplateInnerItem;
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Bootstrap.Control
{
    [ToolboxData("<{0}:BootstrapButton runat=server></{0}:BootstrapButton>")]
    public class BootstrapButton : WebControl, IPostBackEventHandler
    {
        #region Properties
        
        public SizeEnum Size { get; set; }

        public TypeEnum Type { get; set; }

        public string OnClientClick { get; set; }

        public bool Disable { get; set; }

        public bool Active { get; set; }

        public string Text
        {
            get
            {
                return (string)this.ViewState["Text"];
            }
            set
            {
                this.ViewState.Add("Text", value);
            }
        }

        public string Href
        {
            get
            {
                return (string)this.ViewState["Href"];
            }
            set
            {
                this.ViewState["Href"] = value;
            }
        }

        [DefaultValue(true)]
        public bool AutoPostBack { get; set; }

        #endregion

        #region Events
        
        public event EventHandler ServerOnClick;

        #endregion

        #region Methods
        
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            base.Render(writer);

            BootstrapButtonTemplate template = new BootstrapButtonTemplate();
            template.ItemTemplate = new BootstrapButtonTemplateItem();

            template.ItemTemplate.Type = ValueHelper.GetDefaultValue<TypeEnum>(this.Type, TypeEnum.Default);
            template.ItemTemplate.Text = ValueHelper.GetDefaultValue<string>(this.Text, "Button");
            template.ItemTemplate.Size = ValueHelper.GetDefaultValue<SizeEnum>(this.Size, SizeEnum.Normal);
            template.ItemTemplate.OnClientClick = ValueHelper.GetDefaultValue<string>(this.OnClientClick, string.Empty);
            template.ItemTemplate.Href = !string.IsNullOrEmpty(this.Href) ? "javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('" + this.UniqueID + "', '', false, '', '" + Href + "', false, true))" : AutoPostBack ? "javascript:__doPostBack('" + this.UniqueID + "','')" : string.Empty;

            template.ItemTemplate.ClientID = this.UniqueID;
            template.ItemTemplate.Active = this.Active;
            template.ItemTemplate.Disable = this.Disable;


            writer.Write(template.TransformText());
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            Page.ClientScript.GetPostBackEventReference(this, string.Empty);
        }

        public void RaisePostBackEvent(string eventArgument)
        {
            if (ServerOnClick != null && base.Page.IsPostBack)
                ServerOnClick(this, new EventArgs());
        }

        #endregion

    }
}

